results <- read_excel("Unofficial Results (Ryan McGreal).xlsx")
results <- rename(results, ED_NAME_EN = Riding)
results$ED_NAME_EN <- ridings$ED_NAME_EN
ridings@data <- left_join(ridings@data, results, by = "ED_NAME_EN")
data <- read_excel("Ridings-Census Subdivisions-Overlay-Data.xlsx")
data$ED_NAME_EN <- ridings$ED_NAME_EN
data <- transmute(data, ED_NAME_EN, 
                  Population, `Population Density` = `Avg Pop Density`,
                  `Median Age` = `Avg Median Age`,
                  `Median Income` = `Avg Median income`, `Male Median Income` = `Avg Male_Median income`, `Female Median Income` = `Avg Female_Median income`,
                  `Pct Government Transfer Payments` = `Avg Government transfer payments %`,
                  `Median Commuting Duration` = `Avg Median commuting duration`,
                  `Avg Income Taxes as Pct Income` = `Avg Income taxes as % of total income`,
                  `Pct Population in Low Income` = `Avg Population in low income %`,
                  `Population 25-64`,
                  `25-64_No certificate/diploma/degree`,
                  `25-64_HS diploma/equivalent`,
                  `25-64_Post HS diploma`,
                  `25-64_Apprentice/trades certificate`,
                  `25-64_College certificate/diploma`,
                  `25-64_University <bachelor's`,
                  `25-64_University bachelor's+`,
                  `25-64_University_Bachelor's degree`,
                  `25-64_University_Above bachelor's`
                  )
data.model <- mutate(data,
                     `Prop no certificate/diploma/degree` = `25-64_No certificate/diploma/degree`/`Population 25-64`,
                     `Prop HS diploma/equivalent` = `25-64_HS diploma/equivalent`/`Population 25-64`,
                     `Prop Post HS diploma` = `25-64_Post HS diploma`/`Population 25-64`,
                     `Prop apprentice/trades certificate` = `25-64_Apprentice/trades certificate`/`Population 25-64`,
                     `Prop college certificate/diploma` = `25-64_College certificate/diploma`/`Population 25-64`,
                     `Prop university bachelor` = `25-64_University_Bachelor's degree`/`Population 25-64`,
                     `Prop postgraduate` = `25-64_University_Above bachelor's`/`Population 25-64`)

Join results dataframe to spatial polygons:

ridings@data <- left_join(ridings@data, data.model, by = "ED_NAME_EN")
ridings$Winner <- factor(ridings$Winner)
ridings$Winner <- relevel(ridings$Winner, ref = "Liberal")

Ontario General Election 2018

The 2018 general election in Ontario was held on June 31st, with special/advanced polling weeks in advance.

The election was characterized by a strong disaffection of the incumbent government (Liberal), and perhaps particularly Premier Wynne. On the other hand, Ontario Progressive Conservatives were rocked by scandals, including the resignation of the previous leader Patrick Brown, and the election of a well-known fire-breathing populist, Doug Ford, as the new leader.

Polling in advance of the election suggested that the election was the PCs to lose. Somewhat more surprisingly, the NDP rose from a distant third, to match the PC in a statistical deadheat in the popular preferences see here. The electoral system in Ontario, however, is not proportional representation. Instead, the Premier is the leader of the party that can form a majority government (most MPPs elected) or a workin minority government. This means that individual votes may have no impact at all, depending on where they are cast.

My personal political inclinations are fairly progressive. I have voted for the Liberals at the federal and provincial level. This time I voted NDP, seeing as the Liberals would probably be wiped out. The results of the election were disappointing, if hardly surprising. Electing a politician on the mould of Trump (ill-informed, willfully ignorant, plagued by scandal, and willing to say and do anything to acquire power) is a sad development for the province, in my view.

So how does a highbrow professional data analyst deal with disappointment? By using data to try to understand the story.

I obtained the geography file of Polling Divisions in Ontario from here.

The official results of the election may not be announced in weeks. Unofficial (and effectively final) information was collected by Ryan McGreal here, so I used this one for a quick analysis, as follows.

Geography of the results

A useful first step in data analysis is to map the results.

tmap_mode("view")
## tmap mode set to interactive viewing
pal1 <- c("red", "orange", "green", "blue")
tm_shape(ridings) + tm_polygons(fill = "winner")

qtm(ridings, fill = “Winner”, palette = c(“red”, “orange”, “green”, “blue”)) ```